package com.shortLinked.admin.utils;

import java.util.Random;

import static com.shortLinked.admin.common.biz.user.ColoredOutput.RED;
import static com.shortLinked.admin.common.biz.user.ColoredOutput.RESET;

/**
 * 生成随机数工具类 - 分组ID
 */
public final class RandomUtil {

    public static String generateRandomString() {
        System.out.println(RED+"这里是:generateRandomString"+RESET);
        return generateRandomString(6);
    }

    /**
     * 生成随机分组ID
     *
     * @param length 生成多少位
     * @return 分组ID
     */
    public static String generateRandomString(int length) {
        System.out.println(RED+"这里是:generateRandomString"+RESET);
        String characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
        StringBuilder randomString = new StringBuilder();
        Random random = new Random();
        //随机生成一个数字
        for (int i = 0; i < length; i++) {
            randomString.append(characters.charAt(random.nextInt(characters.length())));
//            append加到末尾
//            characters.charAt(number),返回characters中指定索引位置的字符
//            random.nextInt(characters.length()),随机生成一个0-characters.length()-1的数字
        }

        return randomString.toString();
    }
}
